<template>
  <div class='project-manage-container'>
    <!-- <h2 class="title">
      项目管理
      <span @click="goProjectList">全部项目</span>
    </h2> -->
    <div class="title">
      <div class="tabNav">
        <p @click="changePro('map')"
           :class="{'active':type==='map'}">项目地图</p>
        <p @click="changePro('manage')"
           :class="{'active':type==='manage'}">项目管理</p>
      </div>

      <span @click="goProjectList">全部项目</span>
    </div>
    <div class="project-list-content"
         v-if="type === 'manage'">
      <div class="project-item"
           :class="{'project-item-100':showTopProjectList.list.length<=2}"
           v-for="(item) in showTopProjectList.list"
           :key="item.project_id">
        <img :src="item.img"
             v-if="item.img"
             alt="">
        <img src="../../../assets/project-default.png"
             v-if="!item.img"
             alt="">
        <div class="project-info">
          <p :title="item.project_name"
             class="ellipsis"
             @click="goPage(item)">{{item.project_name}}</p>
          <span :title="item.project_name"
                class="ellipsis">
            项目名称：{{item.project_name}}
          </span>
          <span>
            建筑类型：{{item.buildingTypeName}}
          </span>
          <span>项目经理：{{item.leader_name}}</span>
          <span :title="item.address"
                class="ellipsis">项目地址：{{item.address}}</span>
        </div>
      </div>
      <el-pagination layout="prev, pager, next,total"
                     @current-change="handleCurrentChange"
                     :current-page.sync="requestParam.page"
                     :page-size="requestParam.page_size"
                     :total="showTopProjectList.total_count">
      </el-pagination>
    </div>

    <div v-if="type === 'map'">
      <ProjectMap></ProjectMap>
    </div>
  </div>
</template>
<script>
import ProjectMap from './ProjectMap'
import { ProjectListApi } from "@/services/ProjectCenter";
import { standardCodeApi } from "@/services/StandardCode";
export default {
  data () {
    return {
      mapList: [],
      userInfo: {},
      indexPage: 0,
      currentPage: 0,
      type: 'manage',
      requestParam: {
        conditions: [],
        company_id: '',
        module_id: '',
        province_no: '',
        city_no: '',
        area_no: '',
        is_page: 1,
        page: 1,
        page_size: 4,
      },
      // 建筑类型标准码
      buildingTypeCode: [],
      // 建筑类型标准码map
      buildingTypeMap: new Map(),
      showTopProjectList: {
        list: [],
        total_count: 0
      }
    }
  },
  // props: {
  //   projectList: {
  //     type: Array,
  //     default: () => {
  //       return []
  //     }
  //   }
  // },
  components: {
    ProjectMap
  },
  methods: {
    // 获取建筑类型标准码
    getBuildingTypeCode () {
      return standardCodeApi
        .getStandardCodeData({
          data: {
            code_type: "buildingType"
          }
        })
        .then(res => {
          if (res.code === 0) {
            this.buildingTypeCode = res.data.list;
            window.localStorage.setItem(
              "buildingTypeCode",
              JSON.stringify(res.data.list)
            );
            this.buildingTypeCode.map(item => {
              this.buildingTypeMap.set(item.value, item);
            });
          }
        });
    },
    handleCurrentChange (val) {
      this.requestParam.page = val
      this.getProjectList()
    },


    //获取项目数据(改)
    getProjectList () {
      if (this.type === 'map') { //项目地图
        this.requestParam.module_id = "1"
      } else { //项目管理
        this.requestParam.module_id = "2"
      }
      this.requestParam.company_id = this.userInfo.comp_id
      ProjectListApi.getProjectManageList({
        data: this.requestParam
      }).then(res => {
        if (res.code === 0) {

          this.showTopProjectList = res.data
          this.mapList = res.data.list
          res.data.list.map(item => {
            let buildingType = this.buildingTypeMap.get(item.building_type) || {};
            item.buildingTypeName = buildingType.name;
          });

        }
      });
    },
    changePro (val) {
      this.type = val
      if (this.type === 'manage') {
        this.getProjectList()
      }

    },
    goProjectList () {
      this.$router.push({
        path: '/projectList'
      })
    },
    goPage (item) {
      this.$router.push({
        path: '/operaProject', query: { status: 'detail', project_id: item.project_id }
      });
      // this.$router.push({
      //   path: '/projectDetail',
      //   query: {
      //     id: item.project_id
      //   }
      // })
    }
  },
  created () {
    let userInfoLocal = window.localStorage.getItem("userInfo") || "{}";
    this.userInfo = JSON.parse(userInfoLocal);

    this.getBuildingTypeCode()
    this.getProjectList()
  },
  mounted () {
  }
}
</script>
<style scoped lang='scss'>
.project-manage-container {
  background-color: #fff;
  width: 100%;
  height: 100%;
  .title {
    height: 54px;
    display: flex;
    padding: 20px;
    justify-content: space-between;
    align-items: center;
    font-size: 16px;
    color: rgba($color: #000000, $alpha: 0.85);
    border-bottom: 1px solid #d8d8d8;
    font-weight: bold;
    font-family: PingFangSC-Medium, PingFang SC;
    span {
      font-size: 14px;
      color: #1890ff;
      cursor: pointer;
    }
    .tabNav {
      display: flex;
      p {
        cursor: pointer;
        margin-right: 20px;
      }
    }
    .active {
      color: #1890ff;
    }
  }
  .project-list-content {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    height: calc(100% - 100px);
    .project-item {
      width: 50%;
      height: 50%;
      // height: 142px;
      padding: 5px 20px;
      display: flex;
      justify-content: flex-start;
      align-items: center;

      &:first-child {
        border-right: 1px solid #e5e5e5;
        border-bottom: 1px solid #e5e5e5;
      }
      &:nth-child(2) {
        border-bottom: 1px solid #e5e5e5;
      }
      &:nth-child(3) {
        border-right: 1px solid #e5e5e5;
      }
      // 当项目数小于等于2时，设置样式，使其占满一行
      &.project-item-100 {
        width: 100%;
        border-right: 0;
        &:nth-child(2) {
          border-bottom: 0;
        }
      }
      img {
        width: 80px;
        height: 80px;
        border-radius: 4px;
        margin-right: 12px;
      }
      .project-info {
        width: calc(100% - 92px);
        p {
          font-size: 14px;
          color: rgba($color: #000000, $alpha: 0.85);
          margin-bottom: 12px;
          font-weight: bold;
          cursor: pointer;
          width: 100%;
        }
        span {
          font-size: 12px;
          color: #999;
          display: block;
          margin-bottom: 6px;
          &.ellipsis {
            width: 100%;
          }
          &:last-child {
            margin-bottom: 0;
          }
        }
        .service-step {
          display: flex;
          align-items: center;
          i {
            width: 20px;
            height: 4px;
            display: inline-block;
            background-color: #e5e5e5;
            margin-right: 2px;
            &.active {
              background-color: #13c2c2;
            }
          }
        }
      }
    }
  }
  .el-pagination {
    margin-top: 0;
  }
}
</style>